﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ahi.Entity;
using Ahi.Controller;
using Ahi.Config;
using DevExpress.XtraEditors;
using Ahi.Entity.Enums;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using Ahi.Entity.Interfaces;
using Ahi.Raporlama;

namespace Ahi.Toplanti
{
    public partial class ToplantiListesiForm : Base.BaseForm
    {
        List<ToplantiEntity> coll = new List<ToplantiEntity>();

        public ToplantiListesiForm()
        {
            InitializeComponent();
        }

        protected ToplantiEntity CurrentToplanti
        {
            get
            {
                if (bsToplanti.Current is ToplantiEntity)
                    return bsToplanti.Current as ToplantiEntity;
                return null;
            }
        }

        protected ToplantiEntity.KonuEntity CurrentKonu
        {
            get
            {
                if (bsKonular.Current != null)
                    return bsKonular.Current as ToplantiEntity.KonuEntity;
                return null;
            }
        }

        public override void Hazirla()
        {
            repositoryItemLookUpToplantiTipi.SetEnumList<ToplantiTipi>();
            var uyeler = MongoAdaptor.Instance.GetData<UyeEntity>().Select(x => x.AdSoyad);
            if (uyeler.Count() > 0)
                repositoryItemComboBoxKatilimcilar.Items.AddRange(uyeler.ToList());

            repositoryItemLookUpKomisyon.SetDataSource("KomisyonAdi", "Id", MongoAdaptor.Instance.GetData<KomisyonEntity>());
        }

        public override void Yukle()
        {
            Listele();
        }

        private void Listele()
        {
            coll = MongoAdaptor.Instance.GetData<ToplantiEntity>();
            bsToplanti.DataSource = coll;
        }

        public void Kaydet()
        {
            MongoAdaptor.Instance.Save<ToplantiEntity>(coll);
        }

        private void buttonKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet();
        }

        private void buttonListele_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Listele();
        }

        private void buttonExceleAktar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExceleAktar();
        }

        private void ExceleAktar()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel Dosyaları (*.xls)|*.xls";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                gridViewUyeler.ExportToXls(dlg.FileName, true);
            }
        }

        private void buttonYazdir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Yazdir();
        }

        private void Yazdir()
        {
            RaporEntity rapor = null;
            if (RaporSecimForm.ShowRaporSecim(ref rapor, AppConfig.Current.ToplantiRaporu) == System.Windows.Forms.DialogResult.OK)
            {
                if (rapor.Id == ObjectId.Empty)
                    gridToplantılar.ShowPrintPreview();
                else
                {
                    AppConfig.Current.ToplantiRaporu = rapor.Id;
                    AppConfig.Current.SaveToDatabase();

                    Raporlama.RaporGoruntuleyiciForm frm = new Raporlama.RaporGoruntuleyiciForm();
                    frm.Id = AppConfig.Current.ToplantiRaporu;
                    if (rapor.KayitGosterimSekli == KayitGosterimSekli.Tekil)
                        frm.DataSource = CurrentToplanti;
                    else
                        frm.DataSource = CurrentToplanti;
                    frm.Show();
                }
            }
        }

        private void buttonKapat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void buttonKonuyuKararaKopyala_Click(object sender, EventArgs e)
        {
            KonuyuKararaKopyala();
        }

        private void KonuyuKararaKopyala()
        {
            if (CurrentKonu != null)
            {
                KararEkle(CurrentKonu.Icerik);
            }
        }

        private void KararEkle(string icerik)
        {
            ToplantiEntity.KararEntity yeniKarar = new ToplantiEntity.KararEntity();
            yeniKarar.Vakit = DateTime.Now;
            yeniKarar.KararMetni = icerik;
            bsKararlar.Add(yeniKarar);
            bsKararlar.MoveLast();
        }

        private void gridToplantılar_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Append)
            {
                ToplantiEntity yeni = bsToplanti.AddNew() as ToplantiEntity;
                yeni.Tarih = DateTime.Now;
                gridViewKararlar.MoveLast();
                e.Handled = true;
            }
        }

        private void gridKonular_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Append)
            {
                KonuEkle();
                e.Handled = true;
            }
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.Append:
                    CurrentToplanti.KatilimciSayisi++;
                    gridViewKatılımcılar.MoveLast();
                    break;
                case NavigatorButtonType.Remove:
                    CurrentToplanti.KatilimciSayisi--;
                    break;
            }
            bsToplanti.EndEdit();
        }

        private void gridKararlar_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Append)
            {
                KararEkle();
                e.Handled = true;
            }
        }

        private void KararEkle()
        {
            ToplantiEntity.KararEntity yeni = new ToplantiEntity.KararEntity();
            yeni.Vakit = DateTime.Now;
            bsKararlar.Add(yeni);
            gridViewKararlar.MoveLast();
        }

        private void gridKonular_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                KonuEkle();
        }

        private void KonuEkle()
        {
            ToplantiEntity.KonuEntity yeni = new ToplantiEntity.KonuEntity();
            yeni.Vakit = DateTime.Now;
            bsKonular.Add(yeni);
            gridViewKonular.MoveLast();
        }

        private void gridKararlar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                KararEkle();
        }
    }
}
